package com.example.forum.controller;


import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.forum.entity.TAppuser;
import com.example.forum.entity.TClickzan;
import com.example.forum.service.ITAppuserService;
import com.example.forum.service.ITClickzanService;
import com.example.forum.vo.JsonResult;
import lombok.experimental.Accessors;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author why
 * @since 2020-04-22
 */
@RestController
@RequestMapping("/ClickZan")
public class TClickzanController extends BaseController {

    @Autowired
    private ITClickzanService clickzanService;

    @Autowired
    private ITAppuserService appuserService;

    /**
     * @param themeId        主题Id
     * @param themeorReplyId 主题或评论Idt
     * @param themeType      类型 0:文章  1:评论
     * @return
     */
    @PostMapping("clickZan")
    public JsonResult clickZan(@RequestParam("ThemeId") String themeId,
                               @RequestParam("ThemeorReplyId") String themeorReplyId,
                               @RequestParam("ThemeType") int themeType) {

        //禁用判断
        TAppuser appuser = appuserService.getOne(new QueryWrapper<TAppuser>()
                .eq("isDeleted", 0)
                .eq("id", currentUserId()));
        if (appuser == null) {
            return new JsonResult(HttpStatus.BAD_REQUEST, "用户不存在", null);
        }
        //是否禁用 过期时间大于当前时间
        if (appuser.getIsprohibit() == true && appuser.getExpirationdate().getTime() >= new Date().getTime()) {
            return new JsonResult(HttpStatus.BAD_REQUEST, "您已被禁用", null);
        }
        TClickzan clickzanServiceOne = clickzanService.getOne(new QueryWrapper<TClickzan>()
                .eq("themeId", themeId)
                .eq("themeorReplyId", themeId)
                .eq("creatoruserid", appuser.getId()));
        if (clickzanServiceOne!=null) {
            return new JsonResult(HttpStatus.BAD_REQUEST, "你已经点赞啦", null);
        }
        Boolean click = clickzanService.clickZan(themeId, themeorReplyId, themeType,appuser);
        if (!click) {
            return new JsonResult(HttpStatus.BAD_REQUEST, "点赞失败", null);
        }
        return new JsonResult(HttpStatus.OK, "点赞成功", null);
    }

    /**
     * @param themeId        主题Id
     * @param themeorReplyId 主题或评论Idt
     * @param themeType      类型 0:文章  1:评论
     * @return
     */
    @PostMapping("noClickZan")
    public JsonResult noClickZan(@RequestParam("ThemeId") String themeId,
                               @RequestParam("ThemeorReplyId") String themeorReplyId,
                               @RequestParam("ThemeType") int themeType) {

        //禁用判断
        TAppuser appuser = appuserService.getOne(new QueryWrapper<TAppuser>()
                .eq("isDeleted", 0)
                .eq("id", currentUserId()));
        if (appuser == null) {
            return new JsonResult(HttpStatus.BAD_REQUEST, "用户不存在", null);
        }
        //是否禁用 过期时间大于当前时间
        if (appuser.getIsprohibit() == true && appuser.getExpirationdate().getTime() >= new Date().getTime()) {
            return new JsonResult(HttpStatus.BAD_REQUEST, "您已被禁用", null);
        }
        TClickzan clickzan = clickzanService.getOne(new QueryWrapper<TClickzan>()
                .eq("themeId", themeId)
                .eq("themeorReplyId", themeorReplyId)
                .eq("themeType", themeType)
                .eq("creatorUserId", currentUserId()));
        if (clickzan==null) {
            return new JsonResult(HttpStatus.BAD_REQUEST, "信息未找到", null);
        }
        clickzanService.getBaseMapper().deleteById(clickzan.getId());
        return new JsonResult(HttpStatus.OK, "取消点赞成功", null);
    }

}
